package com.shop.service.impl;

import com.shop.dao.CategoryMapper;
import com.shop.entity.AttributeResult;
import com.shop.entity.Category;
import com.shop.service.CategoryService;
import com.shop.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.groupingBy;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public List<AttributeResult> getCategories() {
        List<AttributeResult> CategoryResult = new ArrayList<>();
        List<Category> CategoryOne = categoryMapper.selectByLevelAndParentIds(Collections.singletonList(0L), 1);
        if (!CollectionUtils.isEmpty(CategoryOne)) {
            List<Long> firstLevelCategoryIds = CategoryOne.stream().map(Category::getCategoryId).collect(Collectors.toList());
            //获取二级分类的数据
            List<Category> CategoryTwo = categoryMapper.selectByLevelAndParentIds(firstLevelCategoryIds, 2);
            if (!CollectionUtils.isEmpty(CategoryTwo)) {
                List<Long> secondLevelCategoryIds = CategoryTwo.stream().map(Category::getCategoryId).collect(Collectors.toList());
                //获取三级分类的数据
                List<Category> CategoryThree = categoryMapper.selectByLevelAndParentIds(secondLevelCategoryIds, 3);
                if (!CollectionUtils.isEmpty(CategoryThree)) {
                    //根据 parentId 将 thirdLevelCategories 分组
                    Map<Long, List<Category>> thirdLevelCategoryMap = CategoryThree.stream().collect(groupingBy(Category::getParentId));
                    List<AttributeResult> secondLevelCategoryVOS = new ArrayList<>();

                    //处理二级分类
                    for (Category secondLevelCategory : CategoryTwo) {
                        AttributeResult secondLevelCategoryVO = new AttributeResult();
                        BeanUtil.copyProperties(secondLevelCategory, secondLevelCategoryVO);

                        //如果该二级分类下有数据则放入 secondLevelCategoryVOS 对象中
                        if (thirdLevelCategoryMap.containsKey(secondLevelCategory.getCategoryId())) {
                            //根据二级分类的id取出thirdLevelCategoryMap分组中的三级分类list
                            List<Category> tempGoodsCategories = thirdLevelCategoryMap.get(secondLevelCategory.getCategoryId());
                            secondLevelCategoryVO.setList((BeanUtil.copyList(tempGoodsCategories, AttributeResult.class)));
                            secondLevelCategoryVOS.add(secondLevelCategoryVO);
                        }
                    }

                    //处理一级分类
                    if (!CollectionUtils.isEmpty(secondLevelCategoryVOS)) {
                        //根据 parentId 将 thirdLevelCategories 分组
                        Map<Long, List<AttributeResult>> secondLevelCategoryVOMap = secondLevelCategoryVOS.stream().collect(groupingBy(AttributeResult::getParentId));
                        for (Category firstCategory : CategoryOne) {
                            AttributeResult result = new AttributeResult();
                            BeanUtil.copyProperties(firstCategory, result);
                            //如果该一级分类下有数据则放入 newBeeMallIndexCategoryVOS 对象中
                            if (secondLevelCategoryVOMap.containsKey(firstCategory.getCategoryId())) {
                                //根据一级分类的id取出secondLevelCategoryVOMap分组中的二级级分类list
                                List<AttributeResult> tempGoodsCategories = secondLevelCategoryVOMap.get(firstCategory.getCategoryId());
                                result.setList(tempGoodsCategories);
                                CategoryResult.add(result);
                            }
                        }
                    }
                }
            }

        } else {
            return null;
        }
        return CategoryResult;
    }

    @Override
    public List getCategoryAllName() {
        List category = categoryMapper.selectIdAndName();
        List result = new ArrayList<>();
        for (Object o : category) {
            Map<String,String> map = (HashMap<String,String>)o;
            HashMap<Integer, String> a = new HashMap<>();
            a.put(Integer.parseInt(String.valueOf(map.get("category_id"))),map.get("category_name"));
            result.add(a);
        }
        return result;
    }

    @Override
    public String getCategoryName(Long categoryId) {
        Category category = categoryMapper.selectByCategoryId(categoryId);
        return category.getCategoryName();
    }
}
